import numpy as np
from sklearn.linear_model import LogisticRegression
# LogisticRegression使用方法可查看网址https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LogisticRegression.html#sklearn.linear_model.LogisticRegression

# 数据准备
# 训练数据集，格式为（时长，效率），其中时长单位为小时
# 效率为[0,1]之间的小数，数值约到表示效率越高
X_train = np.array([(0,0),(2,0.9),(3,0.4),(4,0.9),(5,0.4),(6,0.4),(6,0.8),(6,0.7),(7,0.2),(7.5,0.8), \
                   (7,0.9),(8,0.1),(8,0.6),(8,0.8)])
# y_train为考试结果，0表示不及格，1表示及格
y_train = np.array([0,0,0,1,0,0,1,1,0,1,1,0,1,1])
print('复习情况 X_train:{0} \n {1}'.format(X_train,y_train))

### 填空1：创建并训练逻辑回归模型，将模型命名为logistic ###
logistic = LogisticRegression()
logistic.fit(X_train, y_train)

# 验证数据
x_val = [(3,0.9),(8,0.5),(7,0.2),(4,0.5),(4,0.7)]
y_val = [0,1,0,0,1]


### 填空2：给出验证数据的精度，并输出 ###
predictions_val = logistic.predict(x_val)
accuracy_val = np.sum(predictions_val == y_val) / len(y_val)
print('Validation accuracy: {0:.2f}%'.format(accuracy_val * 100))

# 测试数据
learning = np.array([(8,0.9)])

### 填空3：给出测试数据的是否及格，以及及格和不及格的概率 ###
proba = logistic.predict_proba(learning)
print('Test data probabilities: {0}'.format(proba))